package com.project.lk.nurseapp.nurse.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.login.model.HintBean;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.UserInfoUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.FormBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class EvaluateChatModel extends PostBaseModel {
    public void postLoadJson(String url,String sessionId,String star,String content){
        Random random = new Random();
        int nextInt = random.nextInt();
        FormBody.Builder builder = new FormBody.Builder();
        long currentTimeMillis = System.currentTimeMillis();
        builder.add("signature",postSignToken(currentTimeMillis+"",nextInt+""))
                .add("timestamp",currentTimeMillis+"")
                .add("nonce",nextInt+"")
                .add("userAccountId",new UserInfoUtils().getAccountId())
                .add("sessionId",sessionId)
                .add("star",star)
                .add("content",content);
        getJsonData(url,builder.build());
    }

    @Override
    public List analyzeJson(String json, String url) {
        Log.e("MLOG",url+json);
        try {
            List<HintBean> list=new ArrayList<>();
            HintBean bean = (HintBean) JsonUtils.getJsonUtils()
                    .toObject(new TypeToken<HintBean>() {
                    }.getType(), json);
            if (TextUtils.equals(bean.getMsg(),"success")){
                evaluateSuccess();
            }else {
                evaluateFail();
            }
            ToastUtils.showShortToast(bean.getInfo());//显示提示信息
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.showShortToast("评价失败");
//            codeFail();
        }
        return null;
    }

    @Override
    public void postSuccessOnMainThread(List list, String url) {

    }

    @Override
    public void postFailedOnMainThread(String url) {
        evaluateFail();
    }

    public void evaluateSuccess(){
        bridge.bridge(BaseApi.evaluate_success,null);

    }

    public void evaluateFail(){
        bridge.bridge(BaseApi.evaluate_fail,null);
    }

}
